#include "raft/node.h"

int main() {
  libraft::Options options1(
      "127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:"
      "8905",
      "127.0.0.1", 8901, "data1", "meta1", "log1");
  libraft::RaftNode raft_node1(options1);
  std::shared_ptr<libraft::Raft> raft1 = raft_node1.StartNode("raft_example7/");

  libraft::Options options2(
      "127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:"
      "8905",
      "127.0.0.1", 8902, "data2", "meta2", "log2");
  libraft::RaftNode raft_node2(options2);
  std::shared_ptr<libraft::Raft> raft2 = raft_node2.StartNode("raft_example7/");

  libraft::Options options3(
      "127.0.0.1:8901,127.0.0.1:8902,127.0.0.1:8903,127.0.0.1:8904,127.0.0.1:"
      "8905",
      "127.0.0.1", 8903, "data3", "meta3", "log3");
  libraft::RaftNode raft_node3(options3);
  std::shared_ptr<libraft::Raft> raft3 = raft_node3.StartNode("raft_example7/");
  
  while (1) {
    if (raft1->HasLeader()) {
      break;
    }

    LOG_INFO << "electing leader... sleep 3s";
    std::this_thread::sleep_for(std::chrono::seconds(3));
  }

  LOG_INFO << "delete node 127.0.0.1:8903";
  raft1->RemovePeer("127.0.0.1:8903");
  raft_node3.StopLoop();

  while (1) {
    LOG_INFO << "electing leader after server 2 remove the cluster... sleep 2s";
    std::string server_status;
    raft1->GetServerStatus(&server_status);
    LOG_INFO << server_status;
    std::this_thread::sleep_for(std::chrono::seconds(2));
  }
  return 0;
}